﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace AnalisisDeRiesgos.Models.User
{
    public enum SearchUserType
    {
        ForCI,
        ForName,
        ForLastName
    }

    public class SearchUserModel
    {
        [DisplayName("Buscar")]
        [StringLength(150, ErrorMessage = "El nombre debe ser máximo de 150 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 ñÑáéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        [DisplayName("Buscar por:")]
        public SearchUserType Criteria { get; set; }

        public List<SearchUserInfo> UserInfo
        {
            get
            {
                return userInfo;
            }
        }

        private List<SearchUserInfo> userInfo;
        private riskanalysisEntities entities = new riskanalysisEntities();

        public SearchUserModel()
        {
            SearchUsers();
        }

        public void SearchUsers(String pattern)
        {
            if (String.IsNullOrWhiteSpace(pattern))
                SearchUserByLastName("");
            else
            {
                switch (Criteria)
                {
                    case SearchUserType.ForName:
                        SearchUserByName(pattern);
                        break;
                    case SearchUserType.ForLastName:
                        SearchUserByLastName(pattern);
                        break;
                    case SearchUserType.ForCI:
                        try
                        {
                            int ci = Convert.ToInt32(pattern);

                            SearchUserByCI(ci);
                        }
                        catch (Exception)
                        {
                            userInfo = new List<SearchUserInfo>();
                        }
                        break;
                }
            }

        }

        public void SearchUsers()
        {
            SearchUsers("");
        }

        private void SearchUserByCI(int CI)
        {
            var users = from u in entities.users
                        where u.CI == CI && u.enabled
                        orderby u.CI
                        select u;
            userInfo = new List<SearchUserInfo>();

            foreach (user item in users)
            {
                userInfo.Add(new SearchUserInfo
                {
                    CI = item.CI,
                    FullName = item.firstName + " " + item.lastName + " " + item.secondLastName,
                    UserName = item.username
                });
            }           
        }

        private void SearchUserByName(string pattern)
        {
            var users = from u in entities.users
                        where u.firstName.Contains(pattern) && u.enabled
                        orderby u.firstName
                        select u;
            userInfo = new List<SearchUserInfo>();

            foreach (user item in users)
            {
                userInfo.Add(new SearchUserInfo
                {
                    CI = item.CI,
                    FullName = item.firstName + " " + item.lastName + " " + item.secondLastName,
                    UserName = item.username
                });
            }           
        }

        private void SearchUserByLastName(string pattern)
        {
            var users = from u in entities.users
                        where u.lastName.Contains(pattern) && u.enabled
                        orderby u.lastName
                        select u;
            userInfo = new List<SearchUserInfo>();

            foreach (user item in users)
            {
                userInfo.Add(new SearchUserInfo
                {
                    CI = item.CI,
                    FullName = item.lastName + " " + item.secondLastName + " " + item.firstName,
                    UserName = item.username
                });
            }           
        }
    }
}